<template>
  <div class="app-container">
    <div class="app-top">
      <div class="input-line">
        <span class="input-label">任务名：</span>
        <el-input v-model="tableParam.jobName" placeholder="请输入" style="width:210px" clearable />
      </div>
      <div class="input-line">
        <span class="input-label">服务器名称：</span>
        <el-input v-model="tableParam.serviceName" placeholder="请输入" style="width:210px" clearable />
      </div>
      <div class="input-line">
        <span class="input-label">状态：</span>
        <el-select v-model="tableParam.status" placeholder="请选择" style="width:120px">
          <el-option label="全部" :value="null" />
          <el-option label="正常" :value="1" />
          <el-option label="禁用" :value="0" />
        </el-select>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="tableParam.page=1,fetchData()">搜索</el-button>
      </div>
    </div>
    <div class="function" />
    <el-table v-loading="listLoading" :data="list" border :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" tooltip-effect="dark">
      <el-table-column align="center" label="任务名" prop="jobName" />
      <el-table-column align="center" label="服务器名称" prop="serviceName" />
      <el-table-column align="center" label="服务器ip" prop="serviceHost" />
      <el-table-column align="center" label="服务器agent端口" prop="servicePort" />
      <el-table-column align="center" label="服务器是否保留备份">
        <template slot-scope="scope">
          <span v-if="scope.row.serviceIsSave == 0" style="color: #969799">否</span>
          <span v-if="scope.row.serviceIsSave == 1" style="color: #00a754">
            <el-tooltip class="item" effect="dark" :content="scope.row.serviceBackupSaveFile" placement="bottom">
              <div>是</div>
            </el-tooltip>
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="中心是否保留备份">
        <template slot-scope="scope">
          <span v-if="scope.row.centreIsSave == 0" style="color: #969799">否</span>
          <span v-if="scope.row.centreIsSave == 1" style="color: #00a754">
            <el-tooltip class="item" effect="dark" :content="scope.row.centreBackupSaveFile" placement="bottom">
              <div>是</div>
            </el-tooltip>
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备份类型">
        <template slot-scope="scope">
          {{ scope.row.backupType*1===1?'mysql':scope.row.backupType*1===2?'目录下属文件':'文件' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="服务器备份状态" width="110">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.serviceBackupStatus*1===0" type="warning">备份失败</el-tag>
          <el-tag v-if="scope.row.serviceBackupStatus*1===1">成功未上传</el-tag>
          <el-tag v-if="scope.row.serviceBackupStatus*1===2" type=" success">成功已上传</el-tag>
          <el-tag v-if="scope.row.serviceBackupStatus*1===3" type="danger">已删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="中心备份状态" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.centreBackupStatus*1===0" type="warning">备份失败</el-tag>
          <el-tag v-if="scope.row.centreBackupStatus*1===1" type="success">备份成功</el-tag>
          <el-tag v-if="scope.row.centreBackupStatus*1===2" type="info">不参与备份</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="文件大小" prop="backupZipSize" />
      <el-table-column align="center" label="创建时间" prop="createTime" />
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <div class="link">
            <el-link v-if="$store.getters.buttonList.includes('backupData_download')" type="primary" @click="dowload(scope.row)">下载</el-link>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="app-pagination">
      <el-pagination :current-page.sync="tableParam.page" :page-sizes="[10, 20, 30, 40]" :page-size="tableParam.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="sizeData" @current-change="fetchData()" />
      <el-button type="primary" size="mini" icon="el-icon-refresh" @click="fetchData()">刷新</el-button>
    </div>
  </div>
</template>

<script>
import { getTaskBackupInfos } from '@/api/backupData'
import axios from 'axios'
import { getToken } from '@/utils/auth'
export default {
  name: 'User',
  components: {
  },
  data() {
    return {
      list: null,
      listLoading: true,
      tableParam: {
        page: 1,
        pageSize: 10,
        jobName: '',
        serviceName: '',
        status: null
      },
      total: null,
      dialog: false,
      row: null
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    open(key, row) {
      this.row = row
      this[key] = true
    },
    reset(key) {
      this[key] = false
    },
    submit(key) {
      this[key] = false
      this.fetchData()
    },
    sizeData(e) {
      this.tableParam.page = 1
      this.tableParam.pageSize = e
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getTaskBackupInfos(this.tableParam).then(res => {
        this.list = res.data.row
        this.total = res.data.total
        this.listLoading = false
      })
    },
    /** 下载*/
    dowload(row) {
      if (row.centreIsSave * 1 === 0) {
        this.$message.warning('中心服务器未保留备份')
        return
      }
      if (!row.centreBackupSaveFile) {
        this.$message.warning('备份数据已删除')
        return
      }
      axios({
        method: 'post',
        url: process.env.VUE_APP_BASE_API + '/backup/web/task_backup_info/data_file_down', // 请求地址
        headers: {
          'x-auth-token': getToken()
        },
        data: {
          id: row.id
        }, // 参数
        responseType: 'blob' // 表明返回服务器返回的数据类型
      }).then(res => {
        const str = res.headers['content-disposition'].match(/[^\;&]*=[^&]*/g)
        const json = {}
        for (const i in str) {
          const name = str[i].split('=')
          json[name[0]] = name[1]
        }
        const blob = new Blob([res.data])
        const link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = json.filename
        link.click()
        // 释放内存
        window.URL.revokeObjectURL(link.href)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.dialog_info {
  height: calc(70vh - 120px);
}
.item {
  padding: 10px 20px;
}
</style>

